package org.ami.onlineschool.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

import org.ami.onlineschool.dataAccess.SchoolDataAccess;
import org.ami.onlineschool.model.Assignment;
import org.ami.onlineschool.model.Course;
import org.ami.onlineschool.model.Semester;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.io.FileNotFoundException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CourseDashboard extends JPanel {

	private SchoolDataAccess schoolModel;
	private JList lstCourses;
	private JList lstSemesters;
	private JList lstAssg;
	private JButton btnAddAssg;

	private static final Object[] MONTHS;
	private static final Object[] DAYS;
	
	static {
		MONTHS = new Object[12];
		for (int i = 0; i < MONTHS.length; i++) {
			MONTHS[i] = i + 1;
		}

		DAYS = new Object[31];
		for (int i = 0; i < DAYS.length; i++) {
			DAYS[i] = i + 1;
		}
	}
	
	/**
	 * Create the panel.
	 */
	public CourseDashboard() {
		
		setLayout(new GridLayout(0, 3, 0, 0));
		
		JPanel panel_1 = new JPanel();
		add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_4 = new JPanel();
		panel_1.add(panel_4, BorderLayout.NORTH);
		panel_4.setLayout(new GridLayout(0, 3, 0, 0));
		
		JLabel lblNewLabel = new JLabel("Courses");
		panel_4.add(lblNewLabel);
		
		JButton btnAddCourse = new JButton("+");
		panel_4.add(btnAddCourse);
		
		JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane, BorderLayout.CENTER);
		
		lstCourses = new JList();
		lstCourses.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				populateSemesterList();
			}
		});
		scrollPane.setViewportView(lstCourses);
		btnAddCourse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String courseName = JOptionPane.showInputDialog(CourseDashboard.this, "Enter course name");
				if(courseName != null)
				{
					Course course = new Course(courseName);
					getSchoolModel().getSchool().getCourses().add(course);
					UIUtilities.getListModel(lstCourses).addElement(course);
				}
			}
		});
		
		JPanel panel_2 = new JPanel();
		add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_5 = new JPanel();
		panel_2.add(panel_5, BorderLayout.NORTH);
		panel_5.setLayout(new GridLayout(0, 3, 0, 0));
		
		JLabel lblSemesters = new JLabel("Semesters");
		panel_5.add(lblSemesters);
		
		JButton btnAddSemester = new JButton("+");
		panel_5.add(btnAddSemester);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		panel_2.add(scrollPane_1, BorderLayout.CENTER);
		
		lstSemesters = new JList();
		lstSemesters.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				populateAssignmentsList();
			}
		});
		scrollPane_1.setViewportView(lstSemesters);
		btnAddSemester.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addSemesterToCurrentCourse();
			}
		});
		
		JPanel panel_3 = new JPanel();
		add(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_6 = new JPanel();
		panel_3.add(panel_6, BorderLayout.NORTH);
		panel_6.setLayout(new GridLayout(0, 3, 0, 0));
		
		JLabel lblAssignments = new JLabel("Assignments");
		panel_6.add(lblAssignments);
		
		btnAddAssg = new JButton("+");
		panel_6.add(btnAddAssg);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		panel_3.add(scrollPane_2, BorderLayout.CENTER);
		
		lstAssg = new JList();
		lstAssg.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() > 1) {
					UIDefineAssignment defineAssignment = new UIDefineAssignment();
					defineAssignment.setAssignment((Assignment) lstAssg.getSelectedValue());
					defineAssignment.setDirPath(getSchoolModel().getDirPath());
					defineAssignment.setVisible(true);
				}
					
			}
		});
		scrollPane_2.setViewportView(lstAssg);
		btnAddAssg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addAssignmentToCurrentSemester();
			}
		});

	}

	protected void addAssignmentToCurrentSemester() {
		Integer month;
		Integer day;
		
		
		Assignment assg = new Assignment();
		month = (Integer) JOptionPane.showInputDialog(
                this,
                "Select month",
                "Select month",
                JOptionPane.PLAIN_MESSAGE,
                null,
                MONTHS,
                new Integer(1));
		
		day = (Integer) JOptionPane.showInputDialog(
                this,
                "Select day",
                "Select day",
                JOptionPane.PLAIN_MESSAGE,
                null,
                DAYS,
                new Integer(1));
		
		
		if(month == null || day == null)
			return;
		
		assg.setMonth(month);
		assg.setDay(day);
		
		Semester semester = getSelectedSemester();
		if(semester != null) {
			semester.getAssignments().add(assg);
			semester.sortAssignments();
			UIUtilities.getListModel(lstAssg).addElement(assg);
		}
		
	}

	protected void populateAssignmentsList() {
		Semester sem = getSelectedSemester();
		if(sem != null) {
			
			DefaultListModel model = UIUtilities.getListModel(lstAssg);
			model.clear();
			sem.sortAssignments();
			UIUtilities.populateModelFromList(model, sem.getAssignments());
		}
				
	}

	private Semester getSelectedSemester() {
		return (Semester) lstSemesters.getSelectedValue();
	}

	protected void addSemesterToCurrentCourse() {
		Course course = getSelectedCourse();
		if(course != null) {
			Semester semester = new Semester();
			course.getSemesters().add(semester);
			UIUtilities.getListModel(lstSemesters).addElement(semester);
		}
		
	}

	protected void populateSemesterList() {
		Course course = getSelectedCourse();
		if(course != null) {
			DefaultListModel model = UIUtilities.getListModel(lstSemesters);
			model.clear();
			UIUtilities.populateModelFromList(model, course.getSemesters());
		}
		

	}
	

	private Course getSelectedCourse() {
		return (Course) lstCourses.getSelectedValue();
	}

	public SchoolDataAccess getSchoolModel() {
		return schoolModel;
	}

	public void setSchoolModel(SchoolDataAccess schoolModel) {
		this.schoolModel = schoolModel;
		DefaultListModel model = UIUtilities.getListModel(lstCourses);
		model.clear();
		UIUtilities.populateModelFromList(model, getSchoolModel().getSchool().getCourses());	
	}

	public void saveSchoolOnDisk() {
		try {
			getSchoolModel().saveSchoolToDisk();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
}
